/***************************************************************************
创建者：华磊
 开始时间：               : 2020.1１.09
 copyright            : (C) 深圳市华友高科有限公司
 修改说明：（每次有修改就添加一条，带有 修改人，修改时间，修改描述）

 ***************************************************************************
 *   底部视窗测试用代理类
*                                                                         *
 ***************************************************************************/
#include "bottomviewdelegate.h"
#include <QDebug>
#include "hmicommondefine.h"
#include "instructionEnum.h"
#include <QMessageBox>
#include "communicatedelegate.h"

#include "../CommunicateDelegate/filedata.h"
extern FileData fileData;


BottomViewDelegate::BottomViewDelegate(BottomView *bottomViewIn, GlobalData *globalDataIn,
                                       CommunicateNormalDelegate *communicateNormalIn,
                                       QObject* parent):QObject(parent)
{
    communicateNormal=communicateNormalIn;
    globalData = globalDataIn;
    bottomView=bottomViewIn;

    tmpCurrentStatus = -1;

    zeroCheckTimer = new QTimer;
    connect(zeroCheckTimer,SIGNAL(timeout()),this,SLOT(zeroTimeWork()));
    zeroCheckCount = 0;
    initConnect();
}

BottomViewDelegate::~BottomViewDelegate()
{

}

void BottomViewDelegate::zeroTimeWork()
{
    zeroCheckCount++;

    bool check =false;
    if(zeroCheckCount>2)
    {
        MotionDebugInfo infoOut;
        communicateNormal->getDebugInfo_motionServer(infoOut);
        if(infoOut.isVelprofileEmpty && 1==infoOut.motionServerMode)
        {
            check = true;
        }

    }

    if(check)
    {
        zeroCheckTimer->stop();
        zeroCheckCount = 0;
        emit sendStopSignal();
        qDebug()<<"BottomViewDelegate emit sendStopSignal()";
        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("回零完成"));

    }


}

void BottomViewDelegate::initConnect()
{
    connect(bottomView,SIGNAL(button1Signal()),this,SLOT(button1Slot()));
    connect(bottomView,SIGNAL(button2Signal()),this,SLOT(button2Slot()));
    connect(bottomView,SIGNAL(button3Signal()),this,SLOT(button3Slot()));
    connect(bottomView,SIGNAL(button4Signal()),this,SLOT(button4Slot()));
    connect(bottomView,SIGNAL(button5Signal()),this,SLOT(button5Slot()));

    connect(bottomView,SIGNAL(buttonDebugStopSignal()),this,SLOT(buttonDebugSlot()));

    connect(bottomView->runProgramCheckDialog,SIGNAL(setRunAllow(bool)),this,SLOT(setRunAllowSlot(bool)));
}

void BottomViewDelegate::button1Slot()
{
    //emit changeViewIDsignal(MID_VIEW_RUN);//跳转页面//直接控制左側的按鈕了 2017.04.17改

    if(globalData)
    {
//        if(globalData->tryLock(TRYLOCKTIME))
//        {
        CurrentRunFile currentRunFileOut;
        globalData->getCurrentRunFile(currentRunFileOut);
            if(globalData->globalLoginStatus)
            {
                if(0 == currentRunFileOut.runingFileName.size())
                {
//                    globalData->unlock();
                    emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("当前程序文件为空!\n请打开程序文件后再执行"));
                    return;
                }
                else//2017.04.22补充
                {
                    QString nowName = QString::fromStdString(currentRunFileOut.runingFileName[0]);

                    if(nowName != fileData.currentFileName &&
                            globalData->globalCurrentState != ENUM_COMMAND_RUN &&
                            globalData->globalCurrentState != ENUM_COMMAND_CEASE)//2017.04.28 补充
                    {
                        qDebug()<<tr("打开文件与运行文件不一致!\n请稍后再尝试");
                    }


                }

                if(globalData->globalServoState == EM_MOTOR_STATUS_SERVO_ON)//使能开启
                {
                    if(globalData->globalCurrentState != ENUM_COMMAND_RUN)
                    {
                        if(globalData->globalHandState == EM_CONTROL_MODE_T1 || globalData->globalHandState == EM_CONTROL_MODE_T2)
                        {
                            emit sendRunSignal();//直接发送
                        }
                        else
                        {
                            if(globalData->programRunAllow)//判断是否当前
                            {
                                emit sendRunSignal();
                                globalData->programRunAllow = false;
                            }
                            else
                            {
                                //globalData->programRunAllow = true;

                                //emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("请确保机器人周围没有危险障碍!\n再次点击运行按钮运行程序"));
                                //2017 02 27 消息提示由 新界面处理
                                bottomView->runProgramCheckDialog->show();//里面会对globalData->programRunAllow处理
                            }
                        }
                    }
                    else
                    {
                        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("机器正在运行!"));
                    }
                }
                else
                {
//                    globalData->unlock();
                    emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("使能没有启动!"));
                    return;
                }
            }
//            globalData->unlock();
//        }
    }
}

void BottomViewDelegate::button2Slot()
{
    emit sendStopSignal();
}

void BottomViewDelegate::button3Slot()
{
    emit sendPauseSignal();
}

void BottomViewDelegate::button4Slot()
{
    //qDebug()<<"axis" <<globalData->robotAxisCount;
    if(globalData)
    {
//        if(globalData->tryLock(TRYLOCKTIME))
//        {
            if(globalData->globalLoginStatus)
            {
                if(globalData->globalHandState >= EM_CONTROL_MODE_T2)
                {
                    emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("当前模式不可操作\n请切换手动T1模式"));

//                    globalData->unlock();
                    return;
                }

//                if(globalData->globalServoState == EM_MOTOR_STATUS_SERVO_ON)//使能开启
//                {
                    if(globalData->globalCurrentState != ENUM_COMMAND_RUN && globalData->globalCurrentState != ENUM_COMMAND_CEASE)
                    {
                        QMessageBox message(QMessageBox::Question, "提示", "即将回零位！"
                                            , QMessageBox::Yes|QMessageBox::No , NULL);
                        if(message.exec() == QMessageBox::No)
                        {
                            return;
                        }
                        emit sendReturnZeroSignal();
                        zeroCheckTimer->start(1500);
                    }
                    else
                    {
                        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("机器正在运行!\n请点击停止后再回零"));
                    }
//                }
//                else
//                {
//                    emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("使能没有启动!"));
//                }
//            }
//            globalData->unlock();
        }
    }
}

void BottomViewDelegate::button5Slot()
{
    emit changeViewIDsignal();
    emit sendReturnPageSignal();
}

void BottomViewDelegate::buttonDebugSlot()
{
    if(globalData)
    {
        //if(globalData->globalHandRunStatus == EM_DEBUG_MODE_STEP || globalData->globalHandRunStatus == EM_DEBUG_MODE_CONTINUE)
//        if(globalData->tryLock(TRYLOCKTIME))
//        {
            if(globalData->globalHandState ==  EM_CONTROL_MODE_T1|| globalData->globalHandState == EM_CONTROL_MODE_T2)
            {

                qDebug()<<"debug pause";
                emit sendPauseSignal();
            }

//            globalData->unlock();
//        }
    }
}

void BottomViewDelegate::updateViewSlot(int currentStatus)
{
    if(tmpCurrentStatus != currentStatus)
    {
        tmpCurrentStatus = currentStatus;

        bottomView->button1PictureAddress= bottomView->globalPicPath + "bottom_run_n.png";
        bottomView->button2PictureAddress= bottomView->globalPicPath + "bottom_stop_n.png";
        bottomView->button3PictureAddress= bottomView->globalPicPath + "bottom_pause_n.png";
        bottomView->button4PictureAddress= bottomView->globalPicPath + "bottom_returnzero_n.png";
        bottomView->button5PictureAddress= bottomView->globalPicPath + "bottom_return_n.png";

        if(currentStatus == ENUM_COMMAND_RUN)
        {
            bottomView->button1PictureAddress= bottomView->globalPicPath + "bottom_run_p.png";
        }
        else if(currentStatus == ENUM_COMMAND_CEASE)
        {
            bottomView->button3PictureAddress= bottomView->globalPicPath + "bottom_pause_p.png";
        }
        else
        {
            bottomView->button2PictureAddress= bottomView->globalPicPath + "bottom_stop_p.png";//曾经报错
        }
        bottomView->updateView();
    }
}

void BottomViewDelegate::setRunAllowSlot(bool check)
{
//    globalData->lock();
    globalData->programRunAllow = check;
//    globalData->unlock();

    if(check)//true
    {
        button1Slot();//直接运行
    }
}

void BottomViewDelegate::changeLanguageSlot(int language)
{
    if(1 == language)
    {
        translator.load("/agvwork/AgvHmi/baseBag/hmiProject/language/bottomViewDelEnglishLan.qm");
        qApp->installTranslator(&translator);
    }
    else
    {
        qApp->removeTranslator(&translator);
    }
    bottomView->changeLanguage(language);
}
